// Javascript functions.
var shown = -1;

/*
	Function: animResources
	
	Manages the animation and content load of the Resources div. 
	If the div is hidden, loads the resources through jQuery.get().
	If it's not hidden, checks the content owner. If it's the same tweet it's calling the function, hide the div.
	If it's from another tweet, hide the div, load the new content and show the div again.
	
	Parameters:
		id: It's the tweet order number. The first tweet is id=1, and so on.
		twid: The tweet ID, it comes from twitter.
*/
function animResources(id, twid)
{
	var loadimg = '<img src="/images/loader.gif" style="position: absolute; left: 50%; top: 10px;" />'
	var wd = $(".tweets").css("width");
	if ($(".resources").css("left") == "550px")
	{
		// The resources div is showing.
		if(shown == id)
		{
			// The div is from the same tweet is calling us now. Close it.
			$(".resources").animate({left: "0"}, 500, 'swing',
			function ()
			{
				$(".cbox").animate({width: wd}, 500);
				$("#rsc").html("");
			});
			$("#rc"+id).text("Show tweet's attachments");
			shown = -1;
		}
		else
		{
			// The div shown is from another tweet. Hide it, then load it and show it again.
			$("#rc"+shown).text("Show tweet's attachments");
			$(".resources").animate({left: "0"}, 500, 'swing',
			function ()
			{
				$("#rsc").html(loadimg);
				var jq=$.get("http://"+document.domain+"/ajax/resources.php", {tid: twid}, function (data) { $("#rsc").html(data);} );
				jq.error(function () { showMsg("An error has ocurred. Try again or wait a few seconds"); });
				$(".content").animate({scrollTop: $("#tw"+id).position().top - 20}, 500, 'swing');
				$(".resources").css("top", ($("#tw"+id).position().top + 20)+"px");
				$(".resources").animate({left: "550px"}, 500);
			});
			$("#rc"+id).text("Hide tweet's attachments");
			shown = id;
		}
	}
	else
	{
		$("#rsc").html(loadimg);
		var jq=$.get("http://"+document.domain+"/ajax/resources.php", {tid: encodeURI(twid)}, function (data) { $("#rsc").html(data);} );
		jq.error(function () { showMsg("An error has ocurred. Try again or wait a few seconds"); });
		$(".resources").css("top", ($("#tw"+id).position().top + 20)+"px");
		$(".content").animate({scrollTop: $("#tw"+id).position().top - 20}, 500, 'swing');
		$(".cbox").animate({width: "1000px"}, 500, 'swing',
		function ()
		{
			$(".resources").animate({left: "550px"}, 500);
		});
		$("#rc"+id).text("Hide tweet's attachments");
		shown = id;
	}
}

/* 
	Function: collap
	
	Hide or display a resource (not the whole resource div). Uses jQuery animation.
	
	Parameters:
		id: number id of the resource to hide/display.
*/
function collap(id)
{
	$("#prc"+id).toggle(500);
	var col=document.getElementById("col"+id);
	if(col.innerText=="Hide") { col.innerText="Display"; }
	else { col.innerText="Hide"; }
}

/*
	Function: showMsg
	
	Shows a message in top of the page. Hides it after 3 seconds. Uses jQuery animation.
	
	Parameters:
		txt: Message
*/
function showMsg(txt)
{
	$("#popmsg").text(txt);
	$("#popmsg").animate({top: "0px"}, 'fast', 'swing');
	var t = setTimeout("$('#popmsg').animate({top: '-25px'}, 'fast', 'swing');", 5000);
}

/*
	Function: showtweetwindow
	
	Shows the tweet window, and adds some text to it.
	
	Parameters:
		text: Text to add in the window.
		twid: Id of the tweet (for responses).
*/
function showtweetwindow(text, twid)
{
	$(".popoverlay").css("display", "block");
	$(".tweetwin").toggle();
	$("#tid").val(twid);
	$("#text").val(text);
	$("#charcount").text(140 - text.length);
	$("#text").focus();
}

/*
	Function: hidetweetwindow
	
	Hides the tweet window.
*/
function hidetweetwindow()
{
	$(".tweetwin").toggle();
	$(".popoverlay").css("display", "none");
	$("#tload").css("display", "none");
	$(".bmsg").css("display", "none");
	$(".bmsg").text("");
	$("#tf").css("display", "block");
}

/*
	Function: tweetIt
	
	Tweets the tweet. It takes no arguments because they are loaded from the form elements.
*/
function tweetIt()
{
	var text=$("#text").val();
	var rep=$("#tid").val();
	
	if (text.length > 140)
	{
		showMsg("The tweet is too long. 140 char maximum.");
		return false;
	}
	
	$("#tload").css("display", "inline");
	showMsg("Sending tweet...");
	var jq= $.get("http://"+document.domain+"/ajax/tweet.php", {action: "tweet", tid: rep, twText: text}, function (data) {
		data= $.trim(data);
		if (data == "200")
		{
			hidetweetwindow();
			showMsg("Tweet successfully sent");
		}
		else
		{
			alert(data);
			showMsg("There was an error while sending the tweet. Wait a few moments and retry");
			$("#tload").css("display", "none");
		}
	});
	jq.error( function () { showMsg("There was an error while connecting to Twitter. Wait a few seconds and retry"); $("#tload").css("display", "none"); } );
}

/*
	Function: twaction
	
	Sends an action to Twitter. Supports unfavoriting and retweeting.
	
	Parameters:
	
		act: Action to do. Possible values: 'unfav' or 'retweet'.
		twid: Tweet's ID.
*/
function twAction(act, twid)
{
	var str= new Array();
	
	str['unfav']="Deleting favorite...";
	str['retweet']="Retweeting...";
	
	if(str != 'unfav' && str != 'retweet')
	{
		return false;
	}
	
	showMsg(str[act]);
	var jq = $.get("http://"+document.domain+"/ajax/tweet.php", {action: act, tid: twid}, function (data) {
		data= $.trim(data);
		if (data == 200)
		{
			showMsg("Done");
		}
		else
		{
			showMsg("An error has happened. Wait a few moments and retry");
		}
	});
}

/* 
	Function: updateCC
	
	Updates the char count on the tweet box. 
*/
function updateCC()
{
	$("#charcount").text(140-$("#text").val().length);
}

/*
	Function: showMsgBig
	
	Shows a message in a box.
	
	Parameters:
	
		txt: Obvious.
*/
function showMsgBig(txt)
{
	$("#tf").css("display", "none");
	$(".bmsg").css("display", "block");
	showtweetwindow("", 0);
	$(".bmsg").text(txt);
}

/*
	Function: getOut
	
	Gets the user out.
	
	Parameters:
	
		p: can be 'yes' or 'no', indicates whether we should delete all of his data or not.
*/
function getOut(p)
{
	if(p!="yes") { p="no"; }
	var jq = $.get("http://"+document.domain+"/out.php", {perm: p}, function () {
		window.location.reload();
	});
	jq.error(showMsg("Error. Try again or reload."));
}

/*
	Function: refreshTW
	
	Refresh the tweet list.
*/
function refreshTW()
{
	$(".tweets").html('<img src="/images/loader.gif" style="position: absolute; left: 50%; top: 20px;" />');
	var jq=$.get("http://"+document.domain+"/ajax/favlist.php", {dummy: "dum"}, function (data) { $(".tweets").html(data);  $(".tweet").last().toggleClass("lasttweet");});
	jq.error(function () { showMsg("An error has ocurred. Try again or wait a few seconds"); });
}